package com.example.jinmingwu.a180531_service;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.support.annotation.IntDef;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static MainActivity appRef;
    private Button b_call_service,b_exit_service;
    boolean k=false;

    private Button b_playmusic=null;
    private Button b_stopmusic=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        appRef=this;
        b_call_service=(Button)findViewById(R.id.callsystem);
        b_call_service.setOnClickListener(this);
        b_exit_service=(Button)findViewById(R.id.exit);
        b_exit_service.setOnClickListener(this);

        b_playmusic=(Button)findViewById(R.id.play);
        b_playmusic.setOnClickListener(startIt);
        b_stopmusic=(Button)findViewById(R.id.stop);
        b_stopmusic.setOnClickListener(stopIt);
    }

    private ServiceConnection conn;
    private View.OnClickListener startIt=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setTitle("Playing Music");
            startService(new Intent("com.studio.android.START_AUDIO_SERVICE"));
//            final Intent intent=new Intent("com.studio.android.START_AUDIO_SERVICE");
//            startService(intent);
        }
    };

    private View.OnClickListener stopIt=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setTitle("Stop Music");
            stopService(new Intent("com.studio.android.START_AUDIO_SERVICE"));
            finish();
        }
    };

    @Override
    public void onClick(View v) {
        if(v==b_call_service){
            setTitle("Waiting…… Alarm=5");

            Intent intent=new Intent(MainActivity.this,AlarmReceiver.class);
            PendingIntent p_intent=PendingIntent.getBroadcast(MainActivity.this,0,intent,0);
            //PendingIntent是Intent的进一步封装，既包含Intent的描述又是Intent行为的执行

            Calendar calendar=Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND,5);

            //AlarmManger规定了时间间隔到后实现提醒功能
            AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),p_intent);
            //闹钟类型、闹钟执行时间、响应动作
        }
        if(v==b_exit_service){
            setTitle("Stop");

            Intent intent=new Intent(MainActivity.this,AlarmReceiver.class);
            PendingIntent p_intent=PendingIntent.getBroadcast(MainActivity.this,0,intent,0);

            AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
            am.cancel(p_intent);

            finish();
        }
//
//        if(v==b_playmusic){
//            setTitle("Playing Music");
//            startService(new Intent("com.studio.android.START_AUDIO_SERVICE"));
//        }
//        if(v==b_stopmusic){
//            setTitle("Stop Music");
//            stopService(new Intent("com.studio.android.START_AUDIO_SERVICE"));
//            finish();
//        }
    }

    public static MainActivity getApp() {
        return appRef;
    }

    public void btEvent(String s) {
        setTitle(s);
    }
}
